go to previous page   go to home page   go to next page

DecimalFormat numform = new DecimalFormat("0.00"); 
System.out.println( "Num = " + numform.format(13.456) );

Answer:

Num = 13.46

Padding with Zeros

import java.text.*;

class IODemoZeroPadding
{
  public static void main ( String[] args )
  {
    DecimalFormat numform = new DecimalFormat("0000.0000"); 
    System.out.println( "Padding: " + numform.format( 23.15 ) );
  }
}

A number that needs only a few digits will be padded with zeros on the left and right if there are more zeros in the pattern than needed. For example, the program writes out (in the US)

Padding: 0023.1500

This can be useful for tables, where you wish to keep the decimal places aligned.


QUESTION 7:

What does the following fragment write?

DecimalFormat numform = new DecimalFormat("000.0"); 
System.out.println( "Num = " + numform.format(13.456) );